<?php
// +----------------------------------------------------------------------
// | ZanPianCMS
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpiancms.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.zanpiancms.cc>
// +----------------------------------------------------------------------
namespace app\common\model;
use think\Model;
use think\Cache;
class Gb extends Model{
	 protected $auto = [];
	 protected $insert = ['gb_addtime','gb_ip','gb_status','gb_content','gb_intro','gb_uid'];
	 protected $update = ['gb_content'];	
	 public function user(){
		return $this->hasOne('user','user_id','gb_uid');
     }
	 protected function setgbaddtimeAttr($value,$data){
		if ($data['gb_checktime'] || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}
	protected function setGbIntroAttr($value){
		if($value){
			return $value;
		}else{
			return '';
		}
	}		
	protected function setGbUidAttr($value){
		if($value){
			return $value;
		}else{
			return '';
		}
	}	
	 protected function setgbstatusAttr($value){
		if($value){
		     return $value;	
		}else{
		if($GLOBALS['config']['user']['sys']['user_check']==1){
			return 0;
		}else{
			return 1;
			}
	    }
	}	
	protected function setgbipAttr(){
		return ip2long(request()->ip());
	}
    protected function setgbcontentAttr($value){
		if($GLOBALS['config']['user']['sys']['user_replace']){
		    $array = explode('|',$GLOBALS['config']['user']['sys']['user_replace']);
		    $content= trim(str_replace($array,'*',strip_tags($value)));
		}else{
		    $content= strip_tags($value);
		}
		return $content;
	 }
    protected function setgbtitleAttr($value){
		if($GLOBALS['config']['user']['sys']['user_replace']){
		    $array = explode('|',$GLOBALS['config']['user']['sys']['user_replace']);
		    $content= trim(str_replace($array,'*',strip_tags($value)));
		}else{
		    $content= strip_tags($value);
		}
		return $content;
	 }
	public function getGbCatalogs($userid){
		$data = db('gb')->field('gb_cid,count(gb_cid) as count')->where('gb_uid',$userid)->group('gb_cid')->select();
		foreach ($data as $key =>$value){
			$datas[$value['gb_cid']]=$value['count'];
		}
		$list = array(1=>'其他留言',2=>'影片报错',3=>'网站建议',4=>'访问故障');
		foreach ($list as $key =>$value){
			   $catalogs[$key]['list_id']=$key;
			   $catalogs[$key]['list_name']=$value;
			   $catalogs[$key]['count']=$datas[$key];
		}
		return $catalogs;
	}	 
    //数据查询	
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : '*';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='gb_'.$param['order'].' desc';
		}		
		if(empty(order_check($tag['order']))){
			$tag['order']='gb_addtime desc';
		}		
		$where=[];
		if(empty($tag['status'])) {
		   $where['gb_status']=['eq',1];
		}		
	    if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['gb_cid'] =  array('in',$tag['cid']);
		    }else{
			    $where['gb_cid'] = array('eq',$cids[0]);
		    }
	    }
	    if(!empty($tag['uid'])) {
		    $uid = explode(',',trim($tag['uid']));
		    if(count($uid) > 1) {
			    $where['gb_uid'] =  array('in',$tag['uid']);
		    }else{
			    $where['gb_uid'] = array('eq',$uid[0]);
		    }
	    }
	    if(!empty($tag['vid'])) {
		    $vid = explode(',',trim($tag['vid']));
		    if(count($vid) > 1) {
			    $where['gb_vid'] =  array('in',$tag['vid']);
		    }else{
			    $where['gb_vid'] = array('eq',$vid[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['gb_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['gb_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }	
        if(!empty($tag['page'])){
            $url=urldecode(zanpian_list_url($GLOBALS['route'],config('params')));
		    $data = db('gb')->join('user user','user.user_id = gb_uid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false,['path'=>$url]);
			
		}else{
			$limit_str = intval(abs($tag['start'])).",".intval(abs($tag['limit']));
			$data = db('gb')->join('user user','user.user_id = gb_uid','LEFT')->where($where)->order($tag['order'])->limit($limit_str)->select();
		}
		return $data;
    } 	 

}